﻿using System;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Xml;
using DataCenter.Interface.Model;
using Microsoft.Data.ConnectionUI;

namespace DataCenter.Component.Source
{
    public partial class SqlSourceEditForm : Form
    {
        private SqlSourceEditForm()
        {
            InitializeComponent();
        }
        public SqlSourceEditForm(SqlSourceEntity sourceEntity, bool isModify)
            :this()
        {
            this.IsModify = isModify;
            this.SourceEntity = sourceEntity;
        }
        private bool IsModify;
        private SqlSourceEntity SourceEntity;

        private void SqlSourceEditForm_Load(object sender, EventArgs e)
        {
            cboValidation.SelectedIndex = 0;

            if (IsModify)            
            {
                txtComponentName.Text = SourceEntity.ComponentName;
                txtServer.Text = SourceEntity.DataSource;
                txtDatabase.Text = SourceEntity.InitialCatalog;

                if (!SourceEntity.IntegratedSecurity)
                {
                    cboValidation.SelectedIndex = 1;

                    txtUserId.Text = SourceEntity.UserId;
                    txtPassword.Text = SourceEntity.Password;
                }

                ShowConnectionString();
            }
        }

        private void btnOpenDialog_Click(object sender, EventArgs e)
        {
            DataConnectionDialog dialog = new DataConnectionDialog();
            dialog.DataSources.Add(DataSource.SqlDataSource);
            dialog.SelectedDataProvider = DataProvider.SqlDataProvider;
            if (DataConnectionDialog.Show(dialog, this) == DialogResult.OK)
            {
                txtConnectionString.Text = dialog.ConnectionString;
                ParseConnectionString();
            }
        }

        private void ChangeConnectionString(object sender, EventArgs e)
        {
            ShowConnectionString();
        }

        private void cboValidation_SelectedIndexChanged(object sender, EventArgs e)
        {            
            txtUserId.Enabled = (cboValidation.SelectedIndex == 1);
            txtPassword.Enabled = (cboValidation.SelectedIndex == 1);
        }

        private void txtConnectionString_Validated(object sender, EventArgs e)
        {
            string connectionString = txtConnectionString.Text;
            ParseConnectionString();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (Check())
            {
                SourceEntity.ComponentName = txtComponentName.Text.Trim();
                SourceEntity.DataSource = txtServer.Text.Trim();
                SourceEntity.InitialCatalog = txtDatabase.Text.Trim();
                if (cboValidation.SelectedIndex == 0)
                    SourceEntity.IntegratedSecurity = true;
                else
                {
                    SourceEntity.UserId = txtUserId.Text.Trim();
                    SourceEntity.Password = txtPassword.Text.Trim();
                }

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private bool Check()
        {
            bool checkResult = true;
            string errmsg = "";
            Control ctrl = null;

            if (string.IsNullOrEmpty(txtComponentName.Text))
            {
                errmsg = "组件名称不能为空";
                ctrl = txtComponentName;
                checkResult = false;
                goto ShowError;
            }
            if (string.IsNullOrEmpty(txtServer.Text))
            {
                errmsg = "服务器名称不能为空";
                ctrl = txtServer;
                checkResult = false;
                goto ShowError;
            }
            if (string.IsNullOrEmpty(txtDatabase.Text))
            {
                errmsg = "数据库名称不能为空";
                ctrl = txtDatabase;
                checkResult = false;
                goto ShowError;
            }

            if (cboValidation.SelectedIndex == 1)
            {
                if (string.IsNullOrEmpty(txtUserId.Text))
                {
                    errmsg = "用户名不能为空";
                    ctrl = txtUserId;
                    checkResult = false;
                    goto ShowError;
                }
                if (string.IsNullOrEmpty(txtPassword.Text))
                {
                    errmsg = "密码不能为空";
                    ctrl = txtPassword;
                    checkResult = false;
                    goto ShowError;
                }
            }

        ShowError:
            if (!checkResult)
            {
                MessageBox.Show(errmsg, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                if (ctrl != null) ctrl.Focus();
            }
            return checkResult;
        }
        private void ParseConnectionString()
        {
            if (!string.IsNullOrEmpty(txtConnectionString.Text))
            {
                try
                {
                    SqlConnectionStringBuilder cnStrBuilder =
                        new SqlConnectionStringBuilder(txtConnectionString.Text);
                    txtServer.Text = cnStrBuilder.DataSource;
                    txtDatabase.Text = cnStrBuilder.InitialCatalog;
                    if (!cnStrBuilder.IntegratedSecurity)
                    {
                        cboValidation.SelectedIndex = 1;
                        txtUserId.Text = cnStrBuilder.UserID;
                        txtPassword.Text = cnStrBuilder.Password;
                    }
                    else
                    {
                        cboValidation.SelectedIndex = 0;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("解析数据库连接字符串错误\r\n" + ex.Message, 
                        this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        private void ShowConnectionString()
        {
            SqlConnectionStringBuilder cnStrBuilder =
                new SqlConnectionStringBuilder();

            cnStrBuilder.DataSource = txtServer.Text;
            cnStrBuilder.InitialCatalog = txtDatabase.Text;
            if (cboValidation.SelectedIndex == 0)
                cnStrBuilder.IntegratedSecurity = true;
            else
            {
                cnStrBuilder.UserID = txtUserId.Text;
                cnStrBuilder.Password = txtPassword.Text;
            }
            txtConnectionString.Text = cnStrBuilder.ConnectionString;
        }
    }
}